package com.twu.biblioteca;

import java.util.ArrayList;

public class Menu {

    private Inventory inventory;
    private String options;
    private ArrayList<User> users;
    private User currentUser;

    public Menu() {
        inventory = new Inventory();
        options = "1 Book List \n2 Reserve Book \n3 Movie List \n4 Login \n5 Check Membership \nChoose Command by Entering Number:";
        users = new ArrayList<User>();
        currentUser = null;
        User user1, user2;
        user1 = new User(1111111, "password");
        user1.setName("Bobby Digital");
        user1.setEmail("RZA@gmail.com");
        user1.setPhoneNum(1234567890);

        user2 = new User(1111112, "qwerty");
        user2.setName("Dirt McGirt");
        user2.setEmail("ODB@gmail.com");
        user2.setPhoneNum(1234567891);

        addUser(user1);
        addUser(user2);
    }

    public String getMenuOptions() {
        return options;
    }

    public String selectOption(String s) {
        if(s.equals("1")) {
            return inventory.getBookList();
        }
        // s.equals("2") is handled in BibliotecaApp.java
        if(s.equals("2")) {
            return "";
        }
        else if(s.equals("3")) {
            return inventory.getMovieList();
        }
        // s.equals("4") is handled in BibliotecaApp.java
        if(s.equals("4")) {
            return "";
        }
        else if(s.equals("5"))
        {
            return checkMembership();

        }
        return "Select Valid Option";
    }


    public String reserveBook(String s) {


        return inventory.reserveBook(s);
    }

    public String checkMembership() {
        if(currentUser == null)
            return "Please talk to a Librarian. Thank you.";
        return getCurrentUser().printDetails();

    }

    public void addUser(User user) {
        users.add(user);
    }

    public String getUsers() {
        String userList = "";
        for(User u:users)
        {
            userList += u.toString() + "\n";
        }
        return userList.trim();
    }

    public User getCurrentUser() {
        return currentUser;
    }

    public void setCurrentUser(User currentUser) {
        this.currentUser = currentUser;
    }

    public boolean login(int username, String password) {
        for(User user:users)
        {
            if(user.getUsername() == username && user.isValidPassword(password))
            {
                setCurrentUser(user);
                return true;
            }
        }
        return false;  //To change body of created methods use File | Settings | File Templates.
    }
}
